package com.xz.example;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.print.attribute.standard.NumberOfInterveningJobs;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName selfstudy-crawler
 * @title     SoDifficultyDemo   
 * @package    com.xz.example  
 * @date   2019/10/10 14:25  
 * @explain 爬虫小测试
 */
@SuppressWarnings("all")
public class SoDifficultyDemo {

    // 连接池
    static PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();


    {
        // 设置最大连接数
        cm.setMaxTotal(200);

        //  设置每个主机的并发数
        cm.setDefaultMaxPerRoute(20);
    }


    public static void main(String[] args){

        // httpget方式发起请求
        //httpGetCrawler();

        // 带参数的请求
        //httpGetCrawlerByCondition();

        // httppost请求
        //httpPostCrawler();

        // 带参数的httppost请求
        httpPostCrawlerByCondition();
    }


    /**
     * 带参数的HttpPost请求
     */
    private static void httpPostCrawlerByCondition(){

        try {
            // 创建HttpPost请求
            HttpPost httpPost = new HttpPost("http://www.itcast.cn");

            // 声明存放参数的List集合
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("keys","Java"));

            // 创建表单数据entity
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");

            // 设置表单到请求对象中
            httpPost.setEntity(formEntity);

            executeReq(httpPost);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }


    /**
     * httppost请求
     */
    private static void httpPostCrawler() {

        // 创建HttpPost请求
        HttpPost httpPost = new HttpPost("http://www.itcast.cn");
        executeReq(httpPost);
    }


    /**
     *  httpget方式发起请求
     */
    private static void httpGetCrawler() {

        // 创建get请求
        HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
        executeReq(httpGet);
    }


    /**
     * 带参数的请求
     */
    private static void httpGetCrawlerByCondition() {

        // 创建get请求
        HttpGet httpGet = new HttpGet("http://yun.itheima.com/search?keys=Java");
        executeReq(httpGet);
    }


    /**
     * 发起请求的方法
     * @param httpGet
     */
    private static void executeReq(HttpRequestBase httpRequestBase) {

        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        CloseableHttpResponse response = null;

        // 设置请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(1000)//设置创建连接的最长时间
                .setConnectionRequestTimeout(500)//设置获取连接的最长时间
                .setSocketTimeout(10 * 1000)//设置数据传输的最长时间
                .build();

        httpRequestBase.setConfig(requestConfig);

        try {
            // 使用httpclient发起请求
            response = httpClient.execute(httpRequestBase);

            //  判断响应码是否为200
            if (response.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(response.getEntity(),"UTF-8");
                System.out.println(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 释放连接
            if (response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
